<?php

namespace app\api\model;

use traits\model\SoftDelete;

class UserModel extends BaseModel
{
    use SoftDelete;

    protected $pk = 'id';
    protected $table = 'hs_user';
    protected static $tableName = 'user';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';


    //用户状态常量
    const STATUS_FORBID = 0;//禁用
    const STATUS_NORMAL = 1;//启用

    //用户角色常量
    const ROLE_ORDINARY_MEMBER = 1;//普通会员
    const ROLE_RECYCLER = 2;//回收员
    const ROLE_HOUSEKEEPER = 3;//家政员


    public static function status()
    {
        return [
            self::STATUS_FORBID => '禁用',
            self::STATUS_NORMAL => '启用',
        ];
    }

    public static function roles()
    {
        return [
            self::ROLE_ORDINARY_MEMBER => '普通用户',
            self::ROLE_RECYCLER => '会员',
        ];
    }

    public function getQrcodeAttr($value)
    {
        $site_url = BaseModel::siteUrl();
        if ($value) {
            return $site_url . $value;
        }
        return '';
    }

    public function getCreateTimeAttr($value)
    {
        if ($value) {
            return date('Y-m-d H:i:s', $value);
        }
        return '';
    }

    public static function info($user_id)
    {
        return self::where('id', $user_id)->field('id,mobile,nickname,avatar,status,balance,role,ocrnum,c_id')->find();
    }

    public static function getUserByInviteCode($code)
    {
        return self::where('invite_code', $code)->find();
    }

    public static function getUser($id)
    {
        return self::where('id', $id)->find();
    }

    /**
     * 检测用户账号状态
     * @param $user
     * @return array
     */
    public static function checkStatus($user)
    {
        if (!$user->status) {
            return array('code' => -1, 'msg' => '已被禁止，请联系管理员');
        }
        return array('code' => 1);
    }

    /**
     * 更新账号的最后登录时间
     * @param $updateData
     * @param $id
     */
    public static function userLoginTimeUpdate($updateData, $id)
    {
        return UserModel::update($updateData, ['id' => $id]);
    }

    //增加用户余额及财务记录
    public static function money($user_id, $money, $category_name = '')
    {
        $user = self::info($user_id);
        $before = $user['balance'];
        self::where('id', $user_id)->setInc('balance', $money);
        $user = self::info($user_id);
        $after = $user['balance'];
        $data = [
            'user_id' => $user_id,
            'money' => $money,
            'before' => $before,
            'after' => $after,
            'memo' => $category_name,
            'createtime' => time(),
        ];
        if (!UserMoneyLogModel::create($data)) {
            throw new \Exception('增加用户财务记录失败');
        }
    }

    //增加用户余额及财务记录
    public static function redbag($user_id, $redbag, $type, $memo = '')
    {

        $user = self::info($user_id);
        $before = $user['redbag'];
        if (!self::where('id', $user_id)->setInc('redbag', $redbag)) {
            throw new \Exception('增加用户红包失败');
        }
        $user = self::info($user_id);
        $after = $user['redbag'];
        $data = [
            'type' => $type,
            'user_id' => $user_id,
            'redbag' => $redbag,
            'before' => $before,
            'after' => $after,
            'memo' => $memo,
            'createtime' => time(),
        ];
        if (!UserRedbagLogModel::create($data)) {
            throw new \Exception('增加用户红包财务记录失败');
        }
    }

    public static function childList($uid)
    {
        $list = self::where('pid', $uid)->field('nickname,createtime')->select();
        return $list;
    }

}